#!/usr/bin/perl -w
use Getopt::Long;
use strict;

#88888888888888888888888888888888888888888888888888888888888888888888888888888
#change these to suit your environment
my $DATABASE_DIR = '/home/share/database';
my $TEMP_DIR = '/local/databases/pgsql/swap';
				# this should be write accessible to postgres
				# for writing out error.log and swap files

#88888888888888888888888888888888888888888888888888888888888888888888888888888



#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    panda_load_db.pl  --name any_dbname
                      --user any_db_user
                     [--verbose]
                     [--debug]
                     [--exclude_incomplete_genomes]
                     [--debug]
                     [--database_dir /home/share/database]
                     [--temp_dir /home/share/database/temp]
                     [--help]

    This script loads fasta files from
        [--database_dir]/sprot/sprot.dat etc.
        [--database_dir]/nr/nr
        [--database_dir]/incomplete/*
        into a specified postgreSQL database
        using the panda executable.

    --temp_dir is the scratch directory for temporary files panda
        uses to put data into the postgreSQL database.
        The error.log file will also be found here.


USAGE





#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
my $help = '';
my $verbose;
my $debug;
my $db_user;
my $dbname;
my $no_incomplete_genomes;
GetOptions(	'verbose'		=> \$verbose,
			'debug'			=> \$debug,
			'help'			=> \$help,
			'user=s'		=> \$db_user,
			'name=s'		=> \$dbname,
			'database_dir'	=> \$DATABASE_DIR,
			'temp_dir'		=> \$TEMP_DIR,
			'exclude_incomplete_genomes'
							=> \$no_incomplete_genomes,
			);
die $usage if ($help);
die "\nMissing database login argument!\n".$usage unless ($db_user && $dbname);
my $db_login = "dbname=$dbname user=$db_user";

# remove trailing slash
$DATABASE_DIR =~ s/\/$//;
$TEMP_DIR =~ s/\/$//;






#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Options common to all

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
my $base_opts = 	'-l"'.$db_login.'" '.
					'-t"'.$TEMP_DIR.'" '.			#base directory
					'-e"'.$TEMP_DIR.'/error.log" '.	#error log
					'-p20000 ';						#dots every nth sequence

#verbose
if ($verbose)
{
	$base_opts = '-v ' . $base_opts;
}

# debug
my $exe_name = $debug ? './panda_debug ' : './panda ';




#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Incomplete genomes

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
if (!$no_incomplete_genomes)
{
	while (<DATA>)
	{
		chomp;
		die $_ unless /([A-Z]+)\t(.+\.out)/;
		my $file = '/incomplete/'.$2;
		my $tag = $1;
		die $_ unless $tag;
		die $_ unless -f $DATABASE_DIR.$file;

		my $exe =		$exe_name .
						'-r"(\d+)" '.						# regex
						'-f"'. $DATABASE_DIR.$file.'" '.	# fasta file
						'-n"'.$tag.'" '.					# db name
						'-i '.								# do not recreate indices
						'-o '.								# do not remove orphans
#						'-s '.								# do not vacuum
						$base_opts;
	
		print $exe, "\n";
		system $exe;
		print "\n";
	}
}


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	PDB

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
	my $exe =		$exe_name .
					'-r"gi\|(\d+)" '.				# regex
					'-f"'.$DATABASE_DIR.'/pdb/pdbaa_taxid.fa" '.# fasta file
					'-n"pdb" '.						# db name
					$base_opts;
	print $exe, "\n";
	system $exe;
	print "\n";

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	SWISS PROT

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

	$exe =		$exe_name .
					'-r"SP\|([^\|]+)" '.		# regex
					'-f"'.$DATABASE_DIR.'/sprot/sprot_dat.fa" '.# fasta file
					'-n"sp" '.					# db name
					$base_opts;
	print $exe, "\n";
	system $exe;
	print "\n";





#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	TrEMBL

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

	$exe =		$exe_name .
					'-r"SP\|([^\|]+)" '.		# regex
					'-f"'.$DATABASE_DIR.'/sprot/trembl_dat.fa" '.		# fasta file
					'-n"tr" '.					# db name
					$base_opts;
	print $exe, "\n";
	system $exe;
	print "\n";


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	TrEMBL new

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

		$exe =		$exe_name .
					'-r"SP\|([^\|]+)" '.		# regex
					'-f"'.$DATABASE_DIR.'/sprot/trembl_new_dat.fa" '.		# fasta file
					'-n"tnew" '.					# db name
					$base_opts;
	print $exe, "\n";
	system $exe;
	print "\n";



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	NCBI NR

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
		$exe =		$exe_name .
					'-r"gi\|(\d+)" '.				# regex
					'-f"'.$DATABASE_DIR.'/share/databases/nr_08may2002" '.	# fasta file
					'-n"nr" '.					# db name
					'-s '.						# vacuum
					$base_opts;
	print $exe, "\n";
	system $exe;
	print "\n";



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	cleanup

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888









#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	List of incomplete genome names

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
__DATA__
IGACA	A_calcoaceticus.out
IGAFE	A_ferrooxidans.out
IGBAN	B_anthracis.out
IGBBR	B_bronchiseptica.out
IGBCE	B_cepacia.out
IGBCR	B_cereus.out
IGBFR	B_fragilis.out
IGBFU	B_fungorum.out
IGBMA	B_mallei.out
IGBPA	B_parapertussis.out
IGBPE	B_pertussis.out
IGBPS	B_pseudomallei.out
IGBST	B_stearothermophilus.out
IGBSU	B_suis.out
IGCAL	C_albicans.out
IGCAU	C_aurantiacus.out
IGCBU	C_burnetii.out
IGCDI	C_difficile.out
IGCDF	C_diphtheriae.out
IGCHU	C_hutchinsonii.out
IGCHY	C_hydrogenoformans.out
IGCNE	C_neoformans.out
IGCPE	C_perfringens.out
IGCPS	C_psittaci.out
IGCTE	C_tepidum.out
IGDIC	Dictyostelium.out
IGDET	D_ethenogenes.out
IGDHA	D_hafniense.out
IGDVU	D_vulgaris.out
IGEFL	E_faecalis.out
IGEFA	E_faecium.out
IGFAC	F_acidarmanus.out
IGFNU	F_nucleatum.out
IGFSU	F_succinogenes.out
IGGSF	G_sulfurreducens.out
IGLMO	L_monocytogenes.out
IGLPN	L_pneumophila.out
IGMAG	Magnetococcus.out
IGMAV	M_avium.out
IGMBA	M_barkeri.out
IGMBO	M_bovis.out
IGMCA	M_capsulatus.out
IGMHL	M_haemolytica.out
IGMMT	M_magnetotacticum.out
IGMPL	M_maripaludis.out
IGMSM	M_smegmatis.out
IGMTB	M_tuberculosis.out
IGNGR	N_gonorrhoeae.out
IGNME	N_meningitidis.out
IGNPU	N_punctiforme.out
IGPFA	P_falciparum.out
IGPFL	P_fluorescens.out
IGPMA	P_marinus.out
IGPPU	P_putida.out
IGPSY	P_syringae.out
IGPVI	P_vivax.out
IGRCA	R_capsulatus.out
IGREU	R_eutropha.out
IGRPA	R_palustris.out
IGRSP	R_sphaeroides.out
IGRTY	R_typhi.out
IGSYN	Synechococcus.out
IGSAR	S_aromaticivorans.out
IGSAU	S_aureus.out
IGSEN	S_enteritidis.out
IGSEP	S_epidermidis.out
IGSEQ	S_equi.out
IGSMU	S_mutans.out
IGSPO	S_pombe.out
IGSPU	S_putrefaciens.out
IGSPY	S_pyogenes.out
IGSTH	S_thermophilus.out
IGTDE	T_denticola.out
IGTFU	T_fusca.out
IGTRU	T_rubripes.out
IGWSP	W_sp.out
IGXFA	X_fastidiosa-almond.out
IGXFO	X_fastidiosa-oleander.out
IGYTB	Y_pseudotuberculosis.out
